<?php
uses('model');

class statModel extends Model {

    function __construct() {
        parent::__construct();
    }

    function topSale($v) {
        $this->dbconnect();
        $this->db->cachetime(3600*12);

        if ($v['updated'])  $where[] = "a.updated >= {$v['updated']}";
        if ($v['where'])    $where[] = $v['where'];

        if ($where) $where = "WHERE ". JOIN(' AND ', $where);
        if (!$v['limit']) $v['limit'] = 10;

        $result = $this->db->cachesql("SELECT SUM(a.amount) as ct, iname, iid, b.model, b.price1, b.price2 FROM {$this->table['indentd']} a LEFT JOIN {$this->table['items']} b  ON a.iid = b.id $where GROUP BY a.iid ORDER BY ct DESC LIMIT {$v['limit']}");
        if ($v['flush']) $this->db->cacheflush();

        return $result;
    }

    function topIn($v) {
        $this->dbconnect();
        $this->db->cachetime(3600*12);

        if ($v['updated']) $where = "WHERE updated >= {$v['updated']}";
        if (!$v['limit']) $v['limit'] = 10;

        $result = $this->db->cachesql("SELECT SUM(price*amount) as ct, iname, iid FROM {$this->table['indentd']} $where GROUP BY iid ORDER BY ct DESC LIMIT {$v['limit']}");
        if ($v['flush']) $this->db->cacheflush();

        return $result;
    }

    function topIn2($v) {
        $this->dbconnect();
        $this->db->cachetime(3600*12);

        if ($v['updated']) $where = "WHERE updated >= {$v['updated']}";
        if (!$v['limit']) $v['limit'] = 10;

        $result = $this->db->cachesql("SELECT SUM((price-cost)*amount) as ct, iname, iid FROM {$this->table['indentd']} $where GROUP BY iid ORDER BY ct DESC LIMIT {$v['limit']}");
        if ($v['flush']) $this->db->cacheflush();

        return $result;
    }


    function queryData($sql) {
        $this->dbconnect();
        $this->db->cachetime(3600*12);

        $data = $this->db->cachesql($sql);
        if ($v['flush']) $this->db->cacheflush();

        return $data;
    }
}
?>
